//
//  PeixunViewController.m
//  Xiangmu
//
//  Created by 이경인 on 2017/10/11.
//  Copyright © 2017年 LIJINGREN. All rights reserved.
//

#import "PeixunViewController.h"
#import "AFNetworking.h"
#import "UIImageView+WebCache.h"
#import "peixunTableViewCell.h"
#import "xiangqingViewController.h"
@interface PeixunViewController ()<UITableViewDelegate,UITableViewDataSource>{

    NSArray *dateArr;
    UITableView *tableview;

    
}

@end

@implementation PeixunViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.navigationItem.title = @"SUPERSTAR";
    UIBarButtonItem *fanhui = [[UIBarButtonItem alloc]initWithTitle:@"返回" style:UIBarButtonItemStyleDone target:self action:nil];
    self.navigationItem.backBarButtonItem = fanhui;
    dateArr = [NSArray array];
    
    tableview = [[UITableView alloc]initWithFrame:CGRectMake(0, 50, self.view.frame.size.width, self.view.frame.size.height) style: UITableViewStylePlain];
    tableview.delegate=self;
    tableview.dataSource=self;
    [self.view addSubview:tableview];
    [self initwithdate];
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
      static NSString *str = @"peixunTableViewCell";
      peixunTableViewCell *cell = [tableview dequeueReusableCellWithIdentifier:str];
    if (cell==nil) {
        cell = [[[NSBundle mainBundle]loadNibNamed:str owner:self options:nil]lastObject];
        NSDictionary *dict =dateArr[indexPath.row];
        cell.label.text = dict[@"train_name"];
        [cell.image sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://172.24.10.175/workout/Uploads/%@",dict[@"thumb"]]]];
    }
    return cell;
}

- (NSInteger)tableView:(nonnull UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return dateArr.count;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 250;
}
-(void)initwithdate{
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    [manager GET:@"http://172.24.10.175/workout/api.php/lists/mod/training" parameters:nil progress:^(NSProgress * _Nonnull downloadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        dateArr =responseObject;
        [tableview reloadData];
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
    }];
}
-(void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    NSDictionary *dict = dateArr[indexPath.row];
    xiangqingViewController *xiangqingVC = [[xiangqingViewController alloc] init];
    xiangqingVC.ID = dict[@"id"];
    self.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:xiangqingVC animated:YES];
    self.hidesBottomBarWhenPushed = NO;
}
@end

